<?php
class PublicAction extends Action{
	//后台头部
	public function top()
	{
		$this->assign('name',$_SESSION['name']);
		$this->assign('levelName',$_SESSION['levelName']);
		$this->display();
			
	}

	//后台首页侧边栏（管理首页）
	public function siderbari()
	{
		$this->display();
	}

	//后台首页
	public function main()
	{
		$size = sizeof(scandir('./Home/Runtime/Cache/'));
		$this->assign('size',$size-2);
		$this->display();
	}
	
	//清除缓存操作
	public function delete(){
		//检查权限
		if(!in_array(2, explode(',',$_SESSION['permission']))){
			$this->error('权限不够！无法清理前台缓存！');
			exit();
		}
		$_dir = './Home/Runtime/Cache/';
		if(!$_dh = @opendir($_dir)) return;
		while (false !== ($_obj = readdir($_dh))) {
			if($_obj=='.' || $_obj=='..') continue;
			@unlink($_dir.'/'.$_obj);
		}
		closedir($_dh);
		$this->success('清除缓存成功！');
	}

	//内容页面侧边栏
	public function siderbarc()
	{
		$this->display();
	}

	//会员侧边栏
	public function siderbaru()
	{
		$this->display();
	}

	//系统侧边栏
	public function siderbars()
	{
		$this->display();
	}
	
	//登陆界面
	public function login(){
		if(isset($_SESSION[C('USER_AUTH_KEY')])){
			$this->assign("jumpUrl","__APP__/Index");
			$this->error('已经登录过！');
		}
		$this->display();
	}
	
	//生成验证码
	public function verify(){
		import('ORG.Util.Image');
		Image::buildImageVerify(4,1,'png',78,30);
	}
	
	public function checkLogin(){
		if($_SESSION['verify']!=md5($_POST['verify'])){
			$this->assign("jumpUrl","__URL__/login");
			$this->error('验证码错误！');
		}else{
			$manager = M("Manager");
			$map = array();
			$map['name'] = $_POST['admin_user'];
			$map['password'] = md5($_POST['admin_pass']);
			$checkUser = $manager->where($map)->find();
			if(!$checkUser){
				$this->assign("jumpUrl","__URL__/login");
				$this->error("用户名或密码不正确！");
			}else{
				session(C('USER_AUTH_KEY'),$checkUser['id']);
				session('name',$checkUser['name']);
				//更新登陆次数、最后登陆时间、最后登陆ip
				$map = array();
				$map['last_time'] = time();
				$map['last_ip'] = $_SERVER["REMOTE_ADDR"];
				$map['login_count'] = ++$checkUser['login_count'];
				$manager->where("id = ".$checkUser['id'])->save($map);
				//查询等级
				$level = M('Level');
				$levelData = $level->where('id = '.$checkUser['level'])
									->field('level_name,permission')
									->find();
				session('permission',$levelData['permission']);
				session('levelName',$levelData['level_name']);
				$this->assign("jumpUrl","__APP__/Index");
				$this->success("登陆成功！");
			}
		}
	}
	
	//注销登陆
	public function logout(){
		if(isset($_SESSION[C('USER_AUTH_KEY')])){
			session(null);
			$this->assign('jumpUrl',__URL__.'/login');
			$this->success("注销成功！");
		}else{
			$this->assign('jumpUrl',__URL__.'/login');
			$this->error('已经注销！');
		}
	}
}
?>